<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('contact_model');
    }

    public function index() {
        $data['title'] = 'Home';
        $data['path'] = base_url() . 'assets';

        $this->load->view('include/header', $data);
        $this->load->view('home/index', $data);
        $this->load->view('include/footer', $data);
    }

    public function about() {
        $data['title'] = 'About Us';
        $data['path'] = base_url() . 'assets';

        $this->load->view('include/header', $data);
        $this->load->view('home/about', $data);
        $this->load->view('include/footer', $data);
    }

    public function contact() {
        $data['title'] = 'Contact';
        $data['path'] = base_url() . 'assets';
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->database();

        $this->load->view('include/header', $data);
        $this->load->view('home/contact', $data);
        $this->load->view('include/footer', $data);
    }

    public function send_message() {
        $data['title'] = 'Contact';
        $data['path'] = base_url() . 'assets';

        //Load helpers
        $this->load->helper(array('form', 'date'));
        $this->load->library('form_validation');
        //Set tag wrap the error message
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        //Set Validate Rule
        $this->form_validation->set_rules('txt_email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('txt_name', 'Name', 'trim|required');
        $this->form_validation->set_rules('txt_subject', 'Subject', 'trim|required');
        $this->form_validation->set_rules('txt_message', 'Message', 'trim|required');

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('include/header', $data);
            $this->load->view('home/contact', $data);
            $this->load->view('include/footer', $data);
        } else {
            $message = array(
                'created_date' => date("Y-m-d H:i:s"),
                'email' => $this->input->post('txt_email'),
                'name' => $this->input->post('txt_name'),
                'subject' => $this->input->post('txt_subject'),
                'message' => $this->input->post('txt_message'),
            );

            $this->contact_model->set_message($message);
            $this->load->view('include/header', $data);
            $this->load->view('home/contact_success');
            $this->load->view('include/footer', $data);
        }
    }

}

/* End of file home.php */
/* Location: ./application/controllers/home.php */